package com.yq.design.singleton;

import com.yq.design.singleton.inner.Singleton;

/**
 * @ClassName Client
 * @Description 单例
 * @Author yaoqiang
 * @Date 2021/3/8 17:42
 * @Version 1.0
 * <p>
 * 单例的特点：1构造器私有，2实例化变量引用私有，3获取静态实例方法公有
 * 8种：饿汉式（静态常量，静态代码块），懒汉式（线程不安全，线程安全同步方法，线程安全同步代码块），静态内部类，枚举，双重检查
 *
 */
public class Client {
    public static void main(String[] args) {
        enumClass();
        staticInnerClass();
        doubleCheck();
        eStatic();
        eStaticBlock();
        lazyUnSafe();
        lazySafe();
        lazySafeBlock();
    }

    public static void enumClass() {
        com.yq.design.singleton.enumClass.Singleton instance = com.yq.design.singleton.enumClass.Singleton.INSTANCE;
        com.yq.design.singleton.enumClass.Singleton instance2 = com.yq.design.singleton.enumClass.Singleton.INSTANCE;
        System.out.println(instance == instance2);
        System.out.println("enumClass()=>" + instance.hashCode() + "+" + instance2.hashCode());
        instance.say();
    }

    public static void staticInnerClass() {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("staticInnerClass()=>" + instance.hashCode() + "+" + instance2.hashCode());

    }

    public static void doubleCheck() {
        com.yq.design.singleton.doubleCheck.Singleton instance = com.yq.design.singleton.doubleCheck.Singleton.getInstance();
        com.yq.design.singleton.doubleCheck.Singleton instance2 = com.yq.design.singleton.doubleCheck.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("doubleCheck()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

    public static void eStatic() {
        com.yq.design.singleton.eStatic.Singleton instance = com.yq.design.singleton.eStatic.Singleton.getInstance();
        com.yq.design.singleton.eStatic.Singleton instance2 = com.yq.design.singleton.eStatic.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("eStatic()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

    public static void eStaticBlock() {
        com.yq.design.singleton.eStaticBlock.Singleton instance = com.yq.design.singleton.eStaticBlock.Singleton.getInstance();
        com.yq.design.singleton.eStaticBlock.Singleton instance2 = com.yq.design.singleton.eStaticBlock.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("eStatic()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

    public static void lazyUnSafe() {
        com.yq.design.singleton.lazyUnSafe.Singleton instance = com.yq.design.singleton.lazyUnSafe.Singleton.getInstance();
        com.yq.design.singleton.lazyUnSafe.Singleton instance2 = com.yq.design.singleton.lazyUnSafe.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("eStatic()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

    public static void lazySafe() {
        com.yq.design.singleton.lazySafe.Singleton instance = com.yq.design.singleton.lazySafe.Singleton.getInstance();
        com.yq.design.singleton.lazySafe.Singleton instance2 = com.yq.design.singleton.lazySafe.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("eStatic()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

    public static void lazySafeBlock() {
        com.yq.design.singleton.lazySafeBlock.Singleton instance = com.yq.design.singleton.lazySafeBlock.Singleton.getInstance();
        com.yq.design.singleton.lazySafeBlock.Singleton instance2 = com.yq.design.singleton.lazySafeBlock.Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("eStatic()=>" + instance.hashCode() + "+" + instance2.hashCode());
    }

}
